using WMS.RealTime.IServers.Interfaces;
using WMS.RealTime.Domain.DTOs;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;

namespace WMS.RealTime.Servers.Services
{
    /// <summary>
    /// 实时推送服务实现
    /// </summary>
    public class RealTimePushService : IRealTimePushService
    {
        private readonly IDashboardHub _dashboardHub;
        private readonly IServiceProvider _serviceProvider;
        private readonly ILogger<RealTimePushService> _logger;

        public RealTimePushService(
            IDashboardHub dashboardHub,
            IServiceProvider serviceProvider,
            ILogger<RealTimePushService> logger)
        {
            _dashboardHub = dashboardHub;
            _serviceProvider = serviceProvider;
            _logger = logger;
        }

        /// <summary>
        /// 推送今日概况
        /// </summary>
        public async Task PushTodayOverviewAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetTodayOverviewAsync();
                await _dashboardHub.SendTodayOverviewAsync(data);
                _logger.LogInformation("推送今日概况数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送今日概况数据失败");
            }
        }

        /// <summary>
        /// 推送昨日概况
        /// </summary>
        public async Task PushYesterdayOverviewAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetYesterdayOverviewAsync();
                await _dashboardHub.SendYesterdayOverviewAsync(data);
                _logger.LogInformation("推送昨日概况数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送昨日概况数据失败");
            }
        }

        /// <summary>
        /// 推送库存占比
        /// </summary>
        public async Task PushInventoryProportionAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetInventoryProportionAsync();
                await _dashboardHub.SendInventoryProportionAsync(data);
                _logger.LogInformation("推送库存占比数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送库存占比数据失败");
            }
        }

        /// <summary>
        /// 推送入库统计
        /// </summary>
        public async Task PushInboundStatisticsAsync(string timeRange = "7")
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetInboundStatisticsAsync(timeRange);
                await _dashboardHub.SendInboundStatisticsAsync(data);
                _logger.LogInformation($"推送入库统计数据成功，时间范围：{timeRange}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送入库统计数据失败");
            }
        }

        /// <summary>
        /// 推送待办事项
        /// </summary>
        public async Task PushTodoItemsAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetTodoItemsAsync();
                await _dashboardHub.SendTodoItemsAsync(data);
                _logger.LogInformation("推送待办事项数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送待办事项数据失败");
            }
        }

        /// <summary>
        /// 推送告警数据
        /// </summary>
        public async Task PushAlertDataAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetAlertDataAsync();
                await _dashboardHub.SendAlertDataAsync(data);
                _logger.LogInformation("推送告警数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送告警数据失败");
            }
        }

        /// <summary>
        /// 推送排名数据
        /// </summary>
        public async Task PushRankingDataAsync()
        {
            try
            {
                using var scope = _serviceProvider.CreateScope();
                var dashboardDataService = scope.ServiceProvider.GetRequiredService<IDashboardDataService>();
                var data = await dashboardDataService.GetRankingDataAsync();
                await _dashboardHub.SendRankingDataAsync(data);
                _logger.LogInformation("推送排名数据成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送排名数据失败");
            }
        }

        /// <summary>
        /// 推送系统消息
        /// </summary>
        public async Task PushSystemMessageAsync(string message)
        {
            try
            {
                await _dashboardHub.SendSystemMessageAsync(message);
                _logger.LogInformation($"推送系统消息成功：{message}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送系统消息失败");
            }
        }

        /// <summary>
        /// 推送所有数据
        /// </summary>
        public async Task PushAllDataAsync()
        {
            try
            {
                _logger.LogInformation("开始推送所有数据");
                
                await Task.WhenAll(
                    PushTodayOverviewAsync(),
                    PushYesterdayOverviewAsync(),
                    PushInventoryProportionAsync(),
                    PushInboundStatisticsAsync(),
                    PushTodoItemsAsync(),
                    PushAlertDataAsync(),
                    PushRankingDataAsync()
                );
                
                _logger.LogInformation("推送所有数据完成");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "推送所有数据失败");
            }
        }
    }
} 